define(function(require, exports, module) {
  var log = require('log');
  var tool = require('tool');  
  var $chaxun = null;
  var cigeng = {}; //词根
  
  function youdaoCls(word){
    var url = 'http://dict.youdao.com/search?le=eng&q='+word
    $.ajax({
      url:url,
      success:function(data){
        var $data = $(data);        
        var ss =　"";
        var clsall = "";
        
        var yinbiao = $data.find(".phonetic:first").text();
        ss = ss+"<div class='item'>"+yinbiao+"</div>";
        
        if( cigeng[word] ){ 
          ss += "<div class='item'>"+cigeng[word].explan+"</div>"
        }
        
        var $cls = $data.find(".collinsToggle li");
        if($cls.length>0){
          $cls.each(function(){
            var t = $(this).find("p:first");
            t.find("span").remove();
            t.find("a").attr("href","");
            clsall = clsall + "<div id='cls' class='item'>"+t.html()+"</div>";
          });
          
          if(cigeng[word]==null){
            var wordorg = $cls.find("p b:first").html();
            if(cigeng[wordorg]){
              ss += "<div class='item'>"+cigeng[wordorg].explan+"</div>"
            }
          }
          
          ss = ss +"<div id='cls'><div class='item'>"+$cls.find("p:first").html()+"</div><a href='#' id='more'>更多..</a></div>";
          $chaxun.find("#explan").html(ss);
          $chaxun.find("#explan #more").click(function(){
            if($cls.length==1)
              $chaxun.find("#explan #more").hide();
            else
              $chaxun.find("#explan #cls").html(clsall);
          });
        }else{
          ss+="<div class='item'>"+$data.find(".trans-container").html()+"</div>"
          $chaxun.find("#explan").html(ss);
        }
      },
      error:function(){
        log('onclick load error');
      }
    });
  }
  
  function searchKeypress(event){
    if(event.keyCode==13){
        var word = $(this).val();
        $(this).val("");
        
        $chaxun.find("#explan").html("");
        $chaxun.find("#word").html(word);        
        word = word.replace(" ","+"); ;        
        tool.readWord(word);
        youdaoCls(word);
      }
  }
  
  exports.cha = function(word){
    
    $chaxun.find("#explan").html("");
      
    if(word==null){
      $chaxun.find("#word").html("");
    }else{
      $chaxun.find("#word").html(word);
      word = word.replace(" ","+");      
      tool.readWord(word);
      youdaoCls(word);      
    }
    $.mobile.changePage("#chaxun");
  }
  
  exports.init= function(){
    tool.loadPage("chaxun.html",function(){
      $chaxun=$("#chaxun");    
      $chaxun.find("#searchinput").keypress(searchKeypress);
    }); 
    $.getJSON(tool.rooturl+"cigeng.json",function(result){
      cigeng=result;
    });
  }
});